﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AP.Framework.Infrastructure.Repositories.IRepositories;
using AP.Framework.Infrastructure.Entities;

namespace AP.Web.Areas.Admin.Controllers
{
    public class ProductCategoryController : Controller
    {
        IProductCategory _productCategory;
        public ProductCategoryController(IProductCategory productCategory)
        {
            _productCategory = productCategory;
        }
        public ActionResult Index()
        {
            var productList = _productCategory.GetAll().OrderBy(p => p.Sequence).ToList();
            return View(productList);
        }
        public ActionResult Add()
        {
            ViewBag.CategoryList = _productCategory.GetAll().OrderBy(p => p.Sequence).Select(p => new SelectListItem() { Text = p.Name, Value = p.ID.ToString() }).ToList();
            return View();
        }
        [HttpPost]
        public ActionResult Add(cms_ProductCategory model)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = "Admin";
                model.ModifyTime = DateTime.Now.Date;
                _productCategory.Add(model);
            }
            return RedirectToAction("Index");
        }
        public ActionResult Edit(int productCategoryId)
        {
            cms_ProductCategory model = _productCategory.FindById(productCategoryId);
            ViewBag.CategoryList = _productCategory.GetAll().OrderBy(p => p.Sequence).Select(p => new SelectListItem() { Text = p.Name, Value = p.ID.ToString() }).ToList();
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(cms_ProductCategory model)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = "Admin";
                model.ModifyTime = DateTime.Now.Date;
                _productCategory.Modify(model);
            }
            return RedirectToAction("Index");
        }

        public ActionResult Delete(int productCategoryId)
        {
            cms_ProductCategory model = _productCategory.FindById(productCategoryId);
            _productCategory.Remove(model);
            return RedirectToAction("Index");
        }

    }
}
